package com.hz.tgb.api.invoice.ocr.hehe.util;

import java.math.BigDecimal;
import java.util.*;

/**
 * 通用工具类
 *
 * @author hezhao on 2018/9/27 17:57
 */
public class CommonUtil {

    private CommonUtil() {
        // 私有类构造方法
    }

    public static String toString(Object obj) {
        return (obj == null) ? "" : obj.toString();
    }

    public static Integer toInteger(Object obj) {
        return (obj == null) ? Integer.valueOf(0) : Integer.valueOf(toString(obj));
    }

    public static Long toLong(Object obj) {
        return (obj == null) ? Long.valueOf(0L) : Long.valueOf(toString(obj));
    }

    public static Double toDouble(Object obj) {
        return (obj == null) ? Double.valueOf(0.0D) : Double.valueOf(toString(obj));
    }

    public static Boolean toBoolean(Object obj) {
        return (obj == null) ? Boolean.valueOf(false) : Boolean.valueOf(toString(obj));
    }

    public static Map<String, Object> toMap(Object obj) {
        return (obj == null) ? new HashMap<>() : (Map)obj;
    }

    public static List<Map<String, Object>> toList(Object obj) {
        return (obj == null) ? new ArrayList<>() : (List<Map<String, Object>>)obj;
    }

    public static Double ifNull(Double value){
        return Objects.isNull(value) ? Double.valueOf(0.0D) : value;
    }

    public static BigDecimal ifNull(BigDecimal value){
        return Objects.isNull(value) ? BigDecimal.ZERO : value;
    }

    public static Integer ifNull(Integer value){
        return Objects.isNull(value) ? Integer.valueOf(0) : value;
    }

    public static <T> T ifNull(T value, T defaultValue){
        return Objects.isNull(value) ? defaultValue : value;
    }

}
